using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace XMLTest
{
    public partial class Form1 : Form
    {
        bool err = default;
        string xmlBugPath;
        public Form1()
        {
            InitializeComponent();

            string cur = System.Environment.CurrentDirectory.Replace("bin\\Debug", "");
            xmlBugPath = cur + "xmlBug.xml";





        }

        /// <summary>
        /// 删除节点下的空值
        /// </summary>
        /// <param name="fileName">xml路径</param>
        /// <returns></returns>
        public XmlDocument delNullNode(string fileName)//这个方法并不太好
        {

            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);
            XmlElement root = doc.DocumentElement;
            XmlNodeList list = root.GetElementsByTagName("*");
            //删除xml空值
            for (int i = list.Count - 1; i >= 0; i--)//bug    当多条人员信息，只有身份证号码不为空则会处理错(有时行，有时则不行)
            {
                XmlNode nod = list[i];
                if (string.IsNullOrEmpty(nod.InnerText))
                {
                    nod.ParentNode.RemoveChild(nod);
                }
            }
            return doc;
        }


        /// <summary>
        /// 删除xml文件里面节点里面为空的值，将节点转换为大写
        /// </summary>
        /// <param name="fileName">xml文件路径</param>
        /// <returns>节点全部大写且节点内容不为空，结构与xml相同的部分数据xml</returns>
        public XmlDocument updatexml(string fileName)
        {
            XmlNode nod;
            XmlDocument doc = new XmlDocument();
            XmlDocument docClone = new XmlDocument();
            doc.Load(fileName);

            //创建新xml简介并添加到根
            XmlDeclaration dec = docClone.CreateXmlDeclaration("1.0", "GBK", "yes");
            docClone.AppendChild(dec);


            //递归复制xml结构，处理节点和值
            xmlCloneToUpper(doc, docClone);
            return docClone;
        }
        /// <summary>
        /// 处理xml文档下节点和内容
        /// </summary>
        /// <param name="doc">原xml</param>
        /// <param name="docClone">处理后的xml文档</param>
        public void xmlCloneToUpper(XmlDocument doc, XmlDocument docClone)
        {
            FatherAppendChild(doc.DocumentElement, docClone.DocumentElement, docClone);
        }

        /// <summary>
        /// 递归处理xml文件节点和内容
        /// </summary>
        /// <param name="father">原xml父元素</param>
        /// <param name="fatherClone">复制后xml的父元素</param>
        /// <param name="docClone">转换后的xml文档</param>
        public void FatherAppendChild(XmlElement father, XmlElement fatherClone, XmlDocument docClone)
        {
            XmlElement newElment = docClone.CreateElement(father.Name.ToUpper());
            //if (fatherClone.ParentNode.NodeType == XmlNodeType.Document)//是文档根节点
            if (fatherClone == null)
                docClone.AppendChild(newElment);
            else
                fatherClone.AppendChild(newElment);
            fatherClone = newElment;
            //XmlNodeList(father, fatherClone);
            foreach (XmlNode node in father.ChildNodes)
            {
                //1.该节点下只有文本节点，且文本为空        即</span>或<span></span>             //这种方式处理了上面空值那个bug
                if (node.ChildNodes.Count == 0)
                    continue;
                //2.该节点下不仅有文本节点,还有子节点       即<div><h1>标题</h1></div>
                if (node.ChildNodes.Count > 1)
                {
                    FatherAppendChild(node as XmlElement, newElment, docClone);
                    continue;
                }
                //3.该节点下只有一个文本节点
                if (node.Name.ToLower() == "sfzh" || node.Name.ToLower() == "xb")
                {
                    //3.1.如果节点为sfzh或xb，并且节点下内容为0
                    if (node.InnerText == "0")
                    {
                        fatherClone.AppendChild(newElment);
                        node.InnerText = "00";
                    }
                    //3.2.如果节点为sfzh或xb，且节点为1，则设置内容为空
                    if (node.InnerText == "1")
                    {
                        fatherClone.AppendChild(newElment);
                        continue;
                    }

                }

                //复制当前节点内容，且节点转换为大写
                newElment = docClone.CreateElement(node.Name.ToUpper());
                newElment.InnerText = node.InnerText;
                fatherClone.AppendChild(newElment);
            }
        }

        //循环
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string xmlBugClonePath = xmlBugPath.Replace("xmlBug.xml", "xmlBugClone.xml");
                delNullNode(xmlBugPath).Save(xmlBugClonePath);
            }
            catch (Exception)
            {
                err = true;
                
            }
            finally
            {
                MessageBox.Show(err ? "失败" : "成功");
                err = default;
            }
            
        }
        //递归
        private void button2_Click(object sender, EventArgs e)
        {
            
            try
            {
                XmlDocument docClone = updatexml(xmlBugPath);
                docClone.Save(xmlBugPath.Replace("xmlBug.xml", "xmlBugClone.xml"));
            }
            catch (Exception)
            {
                err = true;
                
            }
            finally
            {
                MessageBox.Show(err ? "失败" : "成功");
                err = default;
            }
            
        }
    }
}
